﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace HARTCalibrationTool.Commands.HART.CustomCommands
{
    public abstract class CustomCommandsBase : HARTCMDsBase
    {
        protected HART135CMDCodeStruct _customCMDsCodeStruct;

        public const byte GETPREADVALUE = 0x01;
        public const byte GETTMPADVALUE = 0x02;
        public const byte GET_CAL_FLG = 0x03;
        public const byte GET_CUR = 0x04;
        public const byte GET_INFO = 0x08;
        public const byte GETPREVALUE = 0x10;
        public const byte GETTEMPVALUE = 0x20;
        public const byte SETPREFACTOR = 0x81;
        public const byte SETTMPFACTOR = 0x82;
        public const byte SETPRERANGE = 0x83;
        public const byte SETTEMPNUM = 0x85;
        public const byte SETFACPARA = 0x86;
        public const byte SETDEVADDR = 0x89;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct HART135CMDCodeStruct
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
            public byte[] preamble;
            public byte start;
            public byte addressByte1;
            public byte addressByte2;
            public byte addressByte3;
            public byte addressByte4;
            public byte addressByte5;
            public byte cmdID;
            public byte size;
            public CustomCMDsCodeStruct customCMD;
            public byte parity;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct CustomCMDsCodeStruct
        {
            public byte header1;
            public byte header2;
            public byte cmd;
            public byte length;
            public List<byte> data;
            public byte checkSum;
            public byte tail;
        }

        public CustomCommandsBase(SerialPortsBase serialPortManager, string commandName) : base(serialPortManager, commandName)
        {
            _customCMDsCodeStruct = new HART135CMDCodeStruct()
            {
                preamble = new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff },
                start = 0x82,
                addressByte1 = 0xA2,
                addressByte2 = 0x82,
                addressByte3 = 0xFF,
                addressByte4 = 0xFF,
                addressByte5 = 0xFF,
                cmdID = 0x87,
                size = 0,
                customCMD = new CustomCMDsCodeStruct()
                { 
                    header1 = 0x55,
                    header2 = 0xAA,
                    cmd = 0,
                    length = 0,
                    data = new List<byte>(),
                    checkSum = 0,
                    tail = 0xA5,
                },
                parity = 0,
            };
        }

        protected override async Task SendCMD()
        {
            await Task.Run(() => { });
        }

        public override async Task ParseResponse(byte[] data)
        {
            await Task.Run(() => { });
        }
    }
}
